package bb.lanxing.mvp.model;

import bb.lanxing.model.database.RouteBook;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import rx.Observable;
import rx.Subscriber;

public class SprintLocalNavigationLushuModel extends SprintDeviceNavigationLushuModel {
    public SprintLocalNavigationLushuModel(String str) {
        super(str);
    }

    public SprintLocalNavigationLushuModel() {
    }

    @Override
    public void loadNavigation() {
        loadLushuFromLocal().subscribe((Subscriber<? super List<RouteBook>>) new Subscriber<List<RouteBook>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
                SprintLocalNavigationLushuModel.this.setSprintNavList(null);
            }

            @Override
            public void onNext(List<RouteBook> list) {
                SprintLocalNavigationLushuModel.this.setSprintNavList(list != null ? new ArrayList(list) : null);
            }
        });
    }

    protected Observable<List<RouteBook>> loadLushuFromLocal() {
        return Observable.create(new Observable.OnSubscribe<List<RouteBook>>() {
            @Override
            public void call(Subscriber<? super List<RouteBook>> subscriber) {
                List<RouteBook> all = RouteBook.getAll();
                for (RouteBook routeBook : all) {
                    if (routeBook.getServerId() > 0 && routeBook.getSourceType() == 2 && routeBook.getServerType() == 1) {
                        routeBook.setSourceType(RouteBook.TYPE_GOOGLE);
                        routeBook.setIsUpload(true);
                        routeBook.save();
                    }
                }
                Iterator<RouteBook> it = all.iterator();
                while (it.hasNext()) {
                    if (it.next().getServerId() <= 0) {
                        it.remove();
                    }
                }
                subscriber.onNext(all);
                subscriber.onCompleted();
            }
        });
    }
}
